
#include <BLabel>
#include <BGridLayout>
#include <BGroupBox>
#include <BVBoxLayout>

#include "PointConstraintPanel.h"

PointConstraintPanel::PointConstraintPanel(BWidget* parent) : BWidget(parent)
{
	BGroupBox* point2PointGroupBox = new BGroupBox("Point2Point");
	{
		BLabel* flagLabel = new BLabel("flag");
		_flagComboBox = new BComboBox();
		_flagComboBox->addItem("erp", BPointConstraint::FLAGS_ERP);
		_flagComboBox->addItem("cfm", BPointConstraint::FLAGS_ERP);
		_flagComboBox->setEnabled(false);

		BLabel* erpLabel = new BLabel("erp");
		_erpSpinBox = new BRealSpinBox();

		BLabel* cfmLabel = new BLabel("cfm");
		_cfmSpinBox = new BRealSpinBox();

		BGridLayout* gridLayout = new BGridLayout(point2PointGroupBox);
		gridLayout->setWidget(0, 0, flagLabel);
		gridLayout->setWidget(0, 1, _flagComboBox);
		gridLayout->setWidget(1, 0, erpLabel);
		gridLayout->setWidget(1, 1, _erpSpinBox);
		gridLayout->setWidget(2, 0, cfmLabel);
		gridLayout->setWidget(2, 1, _cfmSpinBox);
	}

	BGroupBox* settingGroupBox = new BGroupBox("Setting");
	{
		BLabel* tauLabel = new BLabel("tau");
		_tauSpinBox = new BRealSpinBox();

		BLabel* damplingLabel = new BLabel("Damping");
		_dampingSpinBox = new BRealSpinBox();

		BLabel* _impulseClampLabel = new BLabel("Impulse Clamp");
		_impulseClampSpinBox = new BRealSpinBox();

		BGridLayout* gridLayout = new BGridLayout(settingGroupBox);
		gridLayout->setWidget(0, 0, tauLabel);
		gridLayout->setWidget(0, 1, _tauSpinBox);
		gridLayout->setWidget(1, 0, damplingLabel);
		gridLayout->setWidget(1, 1, _dampingSpinBox);
		gridLayout->setWidget(2, 0, _impulseClampLabel);
		gridLayout->setWidget(2, 1, _impulseClampSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(point2PointGroupBox);
	vlayout->addWidget(settingGroupBox);

	connect(this, Signal_AssetChanged, &PointConstraintPanel::slotAssetChanged);
	connect(_tauSpinBox, Signal_EditFinished, &PointConstraintPanel::slotTauSpinBoxEditFinished);
	connect(_dampingSpinBox, Signal_EditFinished, &PointConstraintPanel::slotDamplingSpinBoxEditFinished);
	connect(_impulseClampSpinBox, Signal_EditFinished, &PointConstraintPanel::slotImpulseClampEditFinished);
}
PointConstraintPanel::~PointConstraintPanel()
{

}

void PointConstraintPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	if (_pointConstraint = value)
	{
		_flagComboBox->setValue((BPointConstraint::Flags)_pointConstraint->flags());
		_erpSpinBox->setValue(_pointConstraint->erp());
		_cfmSpinBox->setValue(_pointConstraint->cfm());
		_tauSpinBox->setValue(_pointConstraint->tau());
		_dampingSpinBox->setValue(_pointConstraint->damping());
		_impulseClampSpinBox->setValue(_pointConstraint->impulseClamp());
	}
}
void PointConstraintPanel::slotTauSpinBoxEditFinished(BObject* object, const BValue& value)
{
	BReal tau = value;
	_pointConstraint->setTAU(tau);
}
void PointConstraintPanel::slotDamplingSpinBoxEditFinished(BObject* object, const BValue& value)
{
	BReal dampling = value;
	_pointConstraint->setDamping(dampling);
}
void PointConstraintPanel::slotImpulseClampEditFinished(BObject* object, const BValue& value)
{
	BReal impulseClamp = value;
	_pointConstraint->setImpulseClamp(impulseClamp);
}

